import os
# file = open(".\\password.txt","w+")
# file_txt = file.read()
# file.close()
def examine(word):  #确认密码附合要求
    if "|" in word:
        word = input("输入内容不得含‘|’")
        word = examine(word)
    if word == "":
        word = input("输入内容不得含为空")
        word = examine(word)
    if len(word >10):
        word = input("不得超十位")
        word = examine(word)
    return(word)
def print_main():  #  打印主界面
    print('='*10 + "密码管理系统" + '='*10)
    print("1、查看所有密码     2、添加密码")
    print("3、删除密码信息     4、退出系统")
    print("输入你要的操作:")

def read_password():   #读取文本存档内容，转化为列表
    file = open(".\\password.txt", "r")
    file_txt = []
    for i in file.readlines():
        if kill_n(i.split("|")) != "":
            file_txt.append(kill_n(i.split("|")))
    # print(file_txt)
    file.close()
    return(file_txt)
def kill_n(word):  #将read_password读取的内容，将列表中的列表内包含的换行去掉   这个功能好像没用。。。
    word_two = []
    for i in word:
        word_two.append(i.replace("\n",""))
    return(word_two)

def read_all():  #功能1，查看密码
    print("{:4}|{:10}|{:10}|{:10}".format("序号","系统名称","账号","密码"))
    for i in range(0,len(read_password())):
        try:
            print("{:5}|{:12}|{:12}|{:12}".format((i+1),read_password()[i][0],read_password()[i][1],read_password()[i][2]))
        except:
            pass
    return (len(read_password()))

def add_password(word):#功能2，写入密码
    file = open(".\\password.txt", "r")
    file_word = file.read()
    if file_word == "":
        file.close()
        file = open(".\\password.txt", "a+")
        file.write(word)
        file.close()
    else:
        file = open(".\\password.txt", "a+")
        file.write("\n" + word)
        file.close()
    print("密码已经存档:")
    read_all()
    #
    #
    # file = open(".\\password.txt","a+")
    # file_word = file.read()
    # if file_word == "":
    #     file.write(word)
    # else:
    #     file.write("\n"+word)
    # print("密码已经存档:")
    # file.close()
    # read_all()

def kill_list(word):
    word_2 = ""
    if "\n" in word:
        word_2 = word[:-2]
    else:
        word_2 = word[:]
    return(word_2)
def del_password(del_number):  #  删除指定序号的数
    file = open(".\\password.txt","r+")
    file_txt = file.readlines()
    # print(file_txt)
    file_txt_two = []
    for i in file_txt:
        file_txt_two.append(kill_list(i))
    del file_txt_two[int(del_number)-1]
    file.close()
    file = open(".\\password.txt", "w+")
    file.write("")
    file.close()
    file = open(".\\password.txt", "a+")
    # print(file_txt_two)
    for i in file_txt_two:
        if i == file_txt_two[-1]:
            file.write(str(i))
        else:
            file.write(str(i)+"\n")
    file.close()

def input_number(word,number = 10): #  输入控制,位数要稳住
    i = str(input(word))
    while len(i) >= int(number):
        i = str(input(word))
        if len(i) <= int(number):
            break
    return(i)

print_main()
while True:
    need_do = str(input("输入指令\n"))
    if need_do == "1":
        read_all()
    if need_do == "2":
        add_password(str(input_number("系统名称"))+"|"+str(input_number("账号"))+"|"+str(input_number("密码")))
    if need_do == "3":
        max_number = read_all()
        while True:
            del_number = int(input("输入要删的数序号"))
            read_all()
            if del_number < max_number:
                break
        # try:
        del_password(int(del_number)-1)
        # except:
        #     print("超删除上限")
            # pass
    if need_do == "4":
        print("已经退出系统")
        break


















print_main()





